]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
ShellApp: Connect applications to systemd journal (if available)
authorColin Walters <walters@verbum.org>
Thu, 7 Nov 2013 17:34:57 +0000 (12:34 -0500)
committerColin Walters <walters@verbum.org>
Thu, 7 Nov 2013 18:44:03 +0000 (13:44 -0500)
Systemd-for-the-user-session would also do this, but that's a deeply
invasive change that I may not actually get to this cycle.  This
change is tiny and non-invasive, but provides an important benefit:
You can actually reliably tell *which* applications are logging which
messages (assuming they're launched by the shell).

This actually complements a recent change in DBus:
See https://bugs.freedesktop.org/show_bug.cgi?id=68559
which does a similar thing for bus activated apps.

https://bugzilla.gnome.org/show_bug.cgi?id=711626

configure.ac
src/shell-app.c

index b37e232e050f2cb971364fe36d041e445c5a825e..8c7e6b170c16c923882344d8008ae001c8536661 100644 (file)
@@ -58,6 +58,21 @@ fi
 
 AM_CONDITIONAL(BUILD_RECORDER, $build_recorder)
 
+AC_ARG_ENABLE([systemd],
+              AS_HELP_STRING([--enable-systemd], [Use systemd]),
+              [enable_systemd=$enableval],
+              [enable_systemd=auto])
+AS_IF([test x$enable_systemd != xno], [
+  AC_MSG_CHECKING([for libsystemd-journal])
+  PKG_CHECK_EXISTS([libsystemd-journal],
+                   [have_systemd=yes
+                    AC_DEFINE([HAVE_SYSTEMD], [1], [Define if we have systemd])],
+                   [have_systemd=no])
+  AC_MSG_RESULT($have_systemd)
+])
+
+AC_MSG_RESULT($enable_systemd)
+
 CLUTTER_MIN_VERSION=1.13.4
 GOBJECT_INTROSPECTION_MIN_VERSION=0.10.1
 GJS_MIN_VERSION=1.38.1
@@ -93,6 +108,9 @@ SHARED_PCS="gio-unix-2.0 >= $GIO_MIN_VERSION
             libnm-glib libnm-util >= $NETWORKMANAGER_MIN_VERSION
             libnm-gtk >= $NETWORKMANAGER_MIN_VERSION
             libsecret-unstable gcr-base-3 >= $GCR_MIN_VERSION"
+if test x$have_systemd = xyes; then
+  SHARED_PCS="${SHARED_PCS} libsystemd-journal"
+fi
 
 PKG_CHECK_MODULES(GNOME_SHELL, $SHARED_PCS)
 PKG_CHECK_MODULES(MUTTER, libmutter >= $MUTTER_MIN_VERSION)
index 9e94e5340d4283a3e3be9fe262299e7a543eeaae..6904b6b6bfb914363562ea4fe113e016c466992c 100644 (file)
 #include "st.h"
 #include "gtkactionmuxer.h"
 
+#ifdef HAVE_SYSTEMD
+#include <systemd/sd-journal.h>
+#include <errno.h>
+#include <unistd.h>
+#endif
+
 typedef enum {
   MATCH_NONE,
   MATCH_SUBSTRING, /* Not prefix, substring */
@@ -1161,6 +1167,29 @@ _gather_pid_callback (GDesktopAppInfo   *gapp,
                                                app);
 }
 
+#ifdef HAVE_SYSTEMD
+/* This sets up the launched application to log to the journal
+ * using its own identifier, instead of just "gnome-session".
+ */
+static void
+app_child_setup (gpointer user_data)
+{
+  const char *appid = user_data;
+  int res;
+  int journalfd = sd_journal_stream_fd (appid, LOG_INFO, FALSE);
+  if (journalfd >= 0)
+    {
+      do
+        res = dup2 (journalfd, 1);
+      while (G_UNLIKELY (res == -1 && errno == EINTR));
+      do
+        res = dup2 (journalfd, 2);
+      while (G_UNLIKELY (res == -1 && errno == EINTR));
+      (void) close (journalfd);
+    }
+}
+#endif
+
 /**
  * shell_app_launch:
  * @timestamp: Event timestamp, or 0 for current event timestamp
@@ -1203,7 +1232,11 @@ shell_app_launch (ShellApp     *app,
   ret = g_desktop_app_info_launch_uris_as_manager (app->info, NULL,
                                                    G_APP_LAUNCH_CONTEXT (context),
                                                    G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
+#ifdef HAVE_SYSTEMD
+                                                   app_child_setup, (gpointer)shell_app_get_id (app),
+#else
                                                    NULL, NULL,
+#endif
                                                    _gather_pid_callback, app,
                                                    error);
   g_object_unref (context);